home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / timer.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  5KB  |  244 lines

  1. /* General purpose software timer facilities
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "timer.h"
  7. #include "proc.h"
  8. #include "mbuf.h"
  9. #include "commands.h"
  10. #include "daemon.h"
  11. #include "hardware.h"
  12. #include "socket.h"
  13.  
  14. /* Head of running timer chain.
  15.  * The list of running timers is sorted in increasing order of expiration;
  16.  * i.e., the first timer to expire is always at the head of the list.
  17.  */
  18. struct timer *Timers;
  19.  
  20. static void t_alarm __ARGS((void *x));
  21.  
  22. /* Process that handles clock ticks */
  23. /* Fixed to solve some timing problems when multiple ticks
  24.  * get handled at once... from Walt Corey, KZ1F
  25.  */
  26. void
  27. timerproc(i,v1,v2)
  28. int i;
  29. void *v1,*v2;
  30. {
  31.     register struct timer *t;
  32.     int i_state;
  33. #ifndef UNIX
  34.     void (**vf)(void);
  35. #endif
  36.  
  37.     for(;;){
  38.     /* Atomic read and decrement of Tick */
  39.     i_state = dirps();
  40.     while(Tick == 0)
  41.         pwait(&Tick);
  42.     Tick = 0;
  43.     restore(i_state);
  44.  
  45.     if(!istate()){
  46.         restore(1);
  47.             tprintf("timer: ints were off!\n");
  48.     }
  49.  
  50. #ifndef UNIX
  51.     /* Call the functions listed in config.c */
  52.         for(vf = Cfunc;*vf != NULL;vf++)
  53.             (*vf)();
  54. #endif
  55.  
  56.         tflush();       /* Flush current session output */
  57.         pwait(NULL);    /* Let them all do their writes */
  58. #ifndef UNIX
  59.         rflush();       /* Flush out buffered console stuff */
  60.         fflush(stdout); /* And flush out stdout too */
  61. #endif
  62.  
  63.         if(Timers == NULLTIMER)
  64.             continue;       /* No active timers, all done */
  65.  
  66.         /* Initialize null expired timer list */
  67.     while((t=Timers)!=NULLTIMER && (t->expiration - Clock) <= 0) { 
  68.         Timers = t->next;
  69.         t->state = TIMER_EXPIRE;
  70.         if(t->func)
  71.         (*t->func)(t->arg);
  72.         }
  73.         pwait(NULL);    /* Let them run before handling more ticks */
  74.     }
  75. }
  76. /* Start a timer */
  77. void
  78. start_timer(t)
  79. struct timer *t;
  80. {
  81.     register struct timer *tnext;
  82.     struct timer *tprev = NULLTIMER;
  83.  
  84.     if(t == NULLTIMER)
  85.         return;
  86.     if(t->state == TIMER_RUN)
  87.         stop_timer(t);
  88.     if(t->duration == 0)
  89.         return;         /* A duration value of 0 disables the timer */
  90.  
  91.     t->next = NULLTIMER;        /* insure forward chain is NULL */
  92.     t->expiration = Clock + t->duration;
  93.     t->state = TIMER_RUN;
  94.  
  95.     /* Find right place on list for this guy. Once again, note use
  96.      * of subtraction and comparison with zero rather than direct
  97.      * comparison of expiration times.
  98.      */
  99.     for(tnext = Timers;tnext != NULLTIMER;tprev=tnext,tnext = tnext->next){
  100.         if((tnext->expiration - t->expiration) >= 0)
  101.             break;
  102.     }
  103.     /* At this point, tprev points to the entry that should go right
  104.      * before us, and tnext points to the entry just after us. Either or
  105.      * both may be null.
  106.      */
  107.     if(tprev == NULLTIMER)
  108.         Timers = t;             /* Put at beginning */
  109.     else
  110.         tprev->next = t;
  111.  
  112.     t->next = tnext;
  113. }
  114. /* Stop a timer */
  115. void
  116. stop_timer(timer)
  117. struct timer *timer;
  118. {
  119.     register struct timer *t;
  120.     struct timer *tlast = NULLTIMER;
  121.  
  122.     if(timer == NULLTIMER || timer->state != TIMER_RUN)
  123.         return;
  124.  
  125.     /* Verify that timer is really on list */
  126.     for(t = Timers;t != NULLTIMER;tlast = t,t = t->next)
  127.         if(t == timer)
  128.             break;
  129.  
  130.     if(t == NULLTIMER)
  131.         return;         /* Should probably panic here */
  132.  
  133.     /* Delete from active timer list */
  134.     if(tlast != NULLTIMER)
  135.         tlast->next = t->next;
  136.     else
  137.         Timers = t->next;       /* Was first on list */
  138.  
  139.     t->state = TIMER_STOP;
  140. }
  141. /* Return milliseconds remaining on this timer */
  142. int32
  143. read_timer(t)
  144. struct timer *t;
  145. {
  146.     int32 remaining;
  147.  
  148.     if(t == NULLTIMER || t->state != TIMER_RUN)
  149.         return 0;
  150.     remaining = t->expiration - Clock;
  151.     if(remaining <= 0)
  152.         return 0;       /* Already expired */
  153.     else
  154.         return remaining * MSPTICK;
  155. }
  156. void
  157. set_timer(t,interval)
  158. struct timer *t;
  159. int32 interval;
  160. {
  161.     if(t == NULLTIMER)
  162.         return;
  163.     /* Round the interval up to the next full tick, and then
  164.      * add another tick to guarantee that the timeout will not
  165.      * occur before the interval is up. This is necessary because
  166.      * we're asynchonous with the system clock.
  167.      */
  168.     if(interval != 0)
  169.         t->duration = 1 + (interval + MSPTICK - 1)/MSPTICK;
  170.     else
  171.         t->duration = 0;
  172. }
  173. /* Delay process for specified number of milliseconds.
  174.  * Normally returns 0; returns -1 if aborted by alarm.
  175.  */
  176. int
  177. pause(ms)
  178. int32 ms;
  179. {
  180.     int val;
  181.  
  182.     if(Curproc == NULLPROC || ms == 0)
  183.         return 0;
  184.     alarm(ms);
  185.     /* The actual event doesn't matter, since we'll be alerted */
  186.     val = 0;
  187.     while(Curproc->alarm.state == TIMER_RUN){
  188.         if((val = pwait(Curproc)) != 0)
  189.             break;
  190.     }
  191.     alarm(0L); /* Make sure it's stopped, in case we were killed */ 
  192.     return (val == EALARM) ? 0 : -1;
  193. }
  194. static void
  195. t_alarm(x)
  196. void *x;
  197. {
  198.     alert((struct proc *)x,EALARM);
  199. }
  200. /* Send signal to current process after specified number of milliseconds */
  201. void
  202. alarm(ms)
  203. int32 ms;
  204. {
  205.     if(Curproc != NULLPROC){
  206.         set_timer(&Curproc->alarm,ms);
  207.         Curproc->alarm.func = t_alarm;
  208.         Curproc->alarm.arg = (char *)Curproc;
  209.         start_timer(&Curproc->alarm);
  210.     }
  211. }
  212. /* Convert time count in seconds to printable days:hr:min:sec format */
  213. char *
  214. tformat(t)
  215. int32 t;
  216. {
  217.     static char buf[17],*cp;
  218.     unsigned int days,hrs,mins,secs;
  219.     int minus;
  220.  
  221.     if(t < 0){
  222.         t = -t;
  223.         minus = 1;
  224.     } else
  225.         minus = 0;
  226.  
  227.     secs = t % 60;
  228.     t /= 60;
  229.     mins = t % 60;
  230.     t /= 60;
  231.     hrs = t % 24;
  232.     t /= 24;
  233.     days = t;
  234.     if(minus){
  235.         cp = buf+1;
  236.         buf[0] = '-';
  237.     } else
  238.         cp = buf;
  239.     sprintf(cp,"%u:%02u:%02u:%02u",days,hrs,mins,secs);
  240.     
  241.     return buf;
  242. }
  243.     
  244.